package cm.cool.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;

import cm.cool.web.bean.News;
import cm.cool.web.service.news.NewsService;
import cm.cool.web.util.respon.PaginationDto;
import cm.cool.web.util.respon.R;

/**
 * 咨讯操作接口
 * @author fangzhenxing
 * time：2017年10月18日14:22:25
 */
@Controller
@RequestMapping("/News")
public class NewsController {
	
	@Autowired
	NewsService newsService;
	
	/**
	 * 按条件查找所有
	 * @param news
	 * @param paginationDto
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/selectByAll")
	public R selectByAll(News news,PaginationDto paginationDto){
		return newsService.selectByAll(news, paginationDto);
	}
	
	
	/**
	 * 添加一个咨讯
	 * @param record
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/insertNews")
	public R insert(News record,HttpServletRequest httpServletRequest){
		return newsService.insertSelective(record,httpServletRequest);
	}
	
	
	/**
	 * 修改一个咨讯
	 * @param record
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/updateNews")
	public R updateNews(News record){
		return newsService.updateByPrimaryKeySelective(record);
	}
	
	/**
	 * 删除一个咨讯
	 * @param news
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/deleteNews")
	public R deleteNews(News news){
		return newsService.deleteByPrimaryKey(news);
	}
	
	@ResponseBody
	@RequestMapping("/createToken")
	public String createToken(){
		return newsService.createToken();
	}
	
	/**
	 * 富文本框上传一个图片
	 * @param file
	 * @param request
	 * @return
	 */
	 @ResponseBody
	 @RequestMapping(value="/uploadIMg")
	  public  String uploadImg(MultipartFile[] files,
	           HttpServletRequest request) {
		 	List<String> resultPath=new ArrayList<String>();
		 	JSONObject resultObject=new JSONObject();
	        resultObject.put("errno ", 0);
	        resultObject.put("data ", resultPath);
		 if (files != null && files.length > 0) {
	            for (int i = 0; i < files.length; i++) {
	                MultipartFile file = files[i];
	                // 保存文件
	               String result=newsService.uploadImg(file, request);
	               resultPath.add(result);
	            }
	        }
	        return  resultObject.toString();
	 }
}
